package com.ruoyi.mobile.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mobile.domain.MedicalRecordPic;
import com.ruoyi.mobile.mapper.MedicalRecordPicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.mapper.MedicalRecordMapper;
import com.ruoyi.mobile.domain.MedicalRecord;
import com.ruoyi.mobile.service.IMedicalRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 病历记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-19
 */
@Service
public class MedicalRecordServiceImpl implements IMedicalRecordService 
{
    @Autowired
    private MedicalRecordMapper medicalRecordMapper;

    @Autowired
    private MedicalRecordPicMapper medicalRecordPicMapper;

    /**
     * 查询病历记录
     * 
     * @param id 病历记录主键
     * @return 病历记录
     */
    @Override
    public MedicalRecord selectMedicalRecordById(Integer id)
    {
        MedicalRecord medicalRecord = medicalRecordMapper.selectMedicalRecordById(id);
        List<String> picList = medicalRecordPicMapper.getPicListByMedicalRecordId(id);
        medicalRecord.setPicList(picList);
        return medicalRecord;
    }

    /**
     * 查询病历记录列表
     * 
     * @param medicalRecord 病历记录
     * @return 病历记录
     */
    @Override
    public List<MedicalRecord> selectMedicalRecordList(MedicalRecord medicalRecord)
    {
        return medicalRecordMapper.selectMedicalRecordList(medicalRecord);
    }

    /**
     * 新增病历记录
     * 
     * @param medicalRecord 病历记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertMedicalRecord(MedicalRecord medicalRecord)
    {
        medicalRecord.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        medicalRecord.setCreateTime(DateUtils.getNowDate());
        medicalRecordMapper.insertMedicalRecord(medicalRecord);
        List<String> picList = medicalRecord.getPicList();
        if (picList != null && !picList.isEmpty()) {
            List<MedicalRecordPic> medicalRecordPics = new ArrayList<>();
            for (String pic: picList) {
                MedicalRecordPic medicalRecordPic = new MedicalRecordPic();
                medicalRecordPic.setUserId(medicalRecord.getUserId());
                medicalRecordPic.setRecordId(medicalRecord.getId());
                medicalRecordPic.setUrl(pic);
                medicalRecordPics.add(medicalRecordPic);
            }
            medicalRecordPicMapper.insertList(medicalRecordPics);
        }

        return 1;
    }

    /**
     * 修改病历记录
     * 
     * @param medicalRecord 病历记录
     * @return 结果
     */
    @Override
    public int updateMedicalRecord(MedicalRecord medicalRecord)
    {
        medicalRecord.setUpdateBy(SecurityUtils.getLoginUser().getUserId().toString());
        medicalRecord.setUpdateTime(DateUtils.getNowDate());
        medicalRecordMapper.updateMedicalRecord(medicalRecord);
        List<String> picList = medicalRecord.getPicList();
        if (picList != null && !picList.isEmpty()) {
            List<MedicalRecordPic> medicalRecordPics = new ArrayList<>();
            for (String pic: picList) {
                MedicalRecordPic medicalRecordPic = new MedicalRecordPic();
                medicalRecordPic.setUserId(medicalRecord.getUserId());
                medicalRecordPic.setRecordId(medicalRecord.getId());
                medicalRecordPic.setUrl(pic);
                medicalRecordPics.add(medicalRecordPic);
            }
            medicalRecordPicMapper.deleteByMedicalRecordId(medicalRecord.getId());
            medicalRecordPicMapper.insertList(medicalRecordPics);
        }
        return 1;
    }

    /**
     * 批量删除病历记录
     * 
     * @param ids 需要删除的病历记录主键
     * @return 结果
     */
    @Override
    public int deleteMedicalRecordByIds(Integer[] ids)
    {
        return medicalRecordMapper.deleteMedicalRecordByIds(ids);
    }

    /**
     * 删除病历记录信息
     * 
     * @param id 病历记录主键
     * @return 结果
     */
    @Override
    public int deleteMedicalRecordById(Integer id)
    {
        return medicalRecordMapper.deleteMedicalRecordById(id);
    }
}
